package controllers;

import helpers.LanguageHelper;
import helpers.ValidateHelper;

import java.io.IOException;
import java.util.Properties;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import debugs.SiteConfigDebug;

import systems.ConfigSystem;

import entities.SiteConfig;
import forms.CaptchaForm;


public class ContactServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		TreeMap values = new TreeMap();
		
		request.setAttribute("check", check);
		request.setAttribute("values", values);
		try {
			request.getRequestDispatcher("/pages/contact.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		TreeMap values = new TreeMap();
		
		TreeMap form = checkValid(request, check);
		check = (TreeMap) form.get("checkValid");
		values = (TreeMap) form.get("values");
		
		check = (TreeMap) CaptchaForm.checkValid(request, check);
		
		if (check.size() == 0) {
			try {
				SiteConfig configs = ConfigSystem.getConfigs();
				Properties props = new Properties();
		        Session session = Session.getDefaultInstance(props, null);
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(configs.getContactEmail(), (String) values.get("email")));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                                configs.getContactEmail(), (String) values.get("email")));
                msg.setSubject("Mail contact from " + (String) values.get("name"));
                msg.setText((String) values.get("content"));
                Transport.send(msg);

	        } catch (AddressException e) {
	                // ...
	        } catch (MessagingException e) {
	                // ...
	        }
			
			request.setAttribute("success", "Thanks for your contact.");
		}
		
		request.setAttribute("check", check);
		request.setAttribute("values", values);
		try {
			request.getRequestDispatcher("/pages/contact.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	private TreeMap checkValid(HttpServletRequest request, TreeMap check) {
		TreeMap form = new TreeMap();
		TreeMap<String, String> values = new TreeMap<String, String>();
		
		String name = (String) request.getParameter("name");
		String email = (String) request.getParameter("email");
		String title = (String) request.getParameter("title");
		String content = (String) request.getParameter("content");
		
		if (name == null || name.isEmpty()) {
			check.put("name", LanguageHelper.getString("contact_name_error_input"));
			values.put("name", "");
		} else {
			values.put("name", name);
		}
		
		if (email == null || email.isEmpty()) {
			check.put("email", LanguageHelper.getString("contact_email_error_input"));
			values.put("email", email);
		} else {
		    if (!ValidateHelper.isEmail(email)) {
		    	check.put("email", LanguageHelper.getString("contact_email_error_valid"));
		    }
		    values.put("email", email);
		}
		
		if (title == null || title.isEmpty()) {
			check.put("title", LanguageHelper.getString("contact_title_error_input"));
			values.put("title", "");
		} else {
			values.put("title", title);
		}
		
		if (content == null || content.isEmpty()) {
			check.put("content", LanguageHelper.getString("contact_content_error_input"));
			values.put("content", "");
		} else {
			values.put("content", content);
		}
		
		form.put("checkValid", check);
		form.put("values", values);
		return form;
	}
}
